package com.taosdata.example.springbootdemo.controller;

import com.taosdata.example.springbootdemo.dao.ParkingLotCountMapper;
import com.taosdata.example.springbootdemo.domain.Weather;
import com.taosdata.example.springbootdemo.service.WeatherService;

import cn.hutool.core.date.LocalDateTimeUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RequestMapping("/weather")
@RestController
public class WeatherController {

    @Autowired
    private WeatherService weatherService;
    
    @Autowired
    ParkingLotCountMapper parkingLotCountMapper;

    @GetMapping("/lastOne")
    public Weather lastOne() {
        return weatherService.lastOne();
    }

    @GetMapping("/init")
    public int init() {
        return weatherService.init();
    }

    @GetMapping("/{limit}/{offset}")
    public List<Weather> queryWeather(@PathVariable Long limit, @PathVariable Long offset) {
        return weatherService.query(limit, offset);
    }

    @PostMapping("/{temperature}/{humidity}")
    public int saveWeather(@PathVariable float temperature, @PathVariable float humidity) {
        return weatherService.save(temperature, humidity);
    }

    @GetMapping("/count")
    public int count() {
        return weatherService.count();
    }

    @GetMapping("/subTables")
    public List<String> getSubTables() {
        return weatherService.getSubTables();
    }

    @GetMapping("/avg")
    public List<Weather> avg() {
        return weatherService.avg();
    }

    @GetMapping("/testParking")
    public List<Map<String, Object>> testParking() {
        return weatherService.testParking();
    }
    /**
     * 剩余车位数历史数据-按车场分
     */

    @GetMapping(value = "/left-count-history-detail")
    public List<Map<String, Object>> leftCountHistoryDetail(
    		@RequestParam(name="projectId", required = true) Long projectId, 
    		@RequestParam(name="lotId", required = false) List<Long> lotIdList,
    		@RequestParam(name="interval", required = false)Integer interval
    		) {

    		LocalDateTime	beginDatetime = LocalDateTime.of(2024,10, 29, 0, 0);

    		LocalDateTime	endDatetime = LocalDateTime.of(2024,10, 30, 23, 0);
    		
    		interval = 3600*1000;
        	List<Map<String, Object>> resultList = weatherService.leftCountHistoryDetail(projectId, lotIdList, beginDatetime, endDatetime, interval);

        	return resultList;
    }
    
    @GetMapping(value = "/left-count-history-detail2")
    public List<Map<String, Object>> leftCountHistoryDetail2(
    		@RequestParam(name="projectId", required = true) Long projectId, 
    		@RequestParam(name="lotId", required = false) List<Long> lotIdList,
    		@RequestParam(name="interval", required = false)Integer interval
    		) {
    	
    	LocalDateTime	beginDatetime = LocalDateTime.of(2024,10, 29, 0, 0);
    	
    	LocalDateTime	endDatetime = LocalDateTime.of(2024,10, 30, 23, 0);
    	
    	interval = 3600*1000;
    	String strBeginDatetime = LocalDateTimeUtil.formatNormal(beginDatetime);
		String strEndDatetime  = LocalDateTimeUtil.formatNormal(endDatetime);;
		List<Map<String, Object>> resultList = parkingLotCountMapper.leftCountHistoryDetail(projectId, lotIdList, strBeginDatetime, strEndDatetime, interval);
    	
    	return resultList;
    }

}
